/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.ServiceType;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.types.ColumnDefinition;
import org.aergia.vinny.gui.util.DialogUtil;

public class ServiceTypePanel extends DatabasePojoPanel {

	private static class ServiceTypePanelModel extends DatabasePojoTableModel<ServiceType> {

		private static List<ColumnDefinition> definitions;
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		static {
			ServiceTypePanelModel.definitions = new ArrayList<ColumnDefinition>();
			ServiceTypePanelModel.definitions.add(new ColumnDefinition("Active", Boolean.class));
			ServiceTypePanelModel.definitions.add(new ColumnDefinition("Name", String.class, 250, 0, javax.swing.SortOrder.ASCENDING));
			ServiceTypePanelModel.definitions.add(new ColumnDefinition("Comment", String.class));
		}

		public ServiceTypePanelModel(final VinnyController controller) {
			super(controller, ServiceTypePanelModel.definitions);
		}

		@Override
		public Object getValueAt(final int rowIndex, final int columnIndex) {
			final ServiceType serviceType = this.records.get(rowIndex);
			switch (columnIndex) {
				case 0:
					return Boolean.valueOf(serviceType.isActive());
				case 1:
					return serviceType.getName();
				case 2:
					return serviceType.getComment();
				default:
					return null;
			}
		}

		@Override
		protected void populateTable() {
			this.records.addAll(this.controller.getServiceTypes());
		}
	}

	private static final long serialVersionUID = 1L;

	/**
	 * @param controller
	 * @param user
	 */
	public ServiceTypePanel(final VinnyController controller, final VinnyUser user) {
		super(controller, user, new ServiceTypePanelModel(controller), true);
	}

	@Override
	protected void addButtonActionPerformed(final ActionEvent event) {
		final ServiceTypeEditor editor = new ServiceTypeEditor(this.controller, this.user);
		final EditorDialog dialog = new EditorDialog(editor);

		if (dialog.isSaved()) {
			final ServiceTypePanelModel serviceTypeModel = (ServiceTypePanelModel) this.tableModel;
			serviceTypeModel.addNewRecord(editor.getServiceType());
			this.refresh();
		}
	}

	@Override
	protected void deleteButtonActionPerformed(final ActionEvent event) {
		final int selectedRow = this.getSelectedRow();
		if (selectedRow == -1) {
			return;
		}
		final ServiceTypePanelModel serviceTypeModel = (ServiceTypePanelModel) this.tableModel;
		final ServiceType serviceType = serviceTypeModel.getRecord(selectedRow);

		if (!DialogUtil.askQuestion(this, "Are you sure you want to disable " + serviceType.getName(), "Disable service type")) {
			return;
		}

		final ApiResult<ServiceType> result = this.controller.deleteServiceType(serviceType, this.user);
		if (!result.isError()) {
			serviceTypeModel.setRecord(selectedRow, serviceType);
			this.tableModel.fireTableRowsUpdated(selectedRow, selectedRow);
			this.refresh();
		} else {
			DialogUtil.alert(this, result.getText(), "Error");
		}

	}

	@Override
	protected void editButtonActionPerformed(final ActionEvent event) {
		final int selectedRow = this.getSelectedRow();
		if (selectedRow == -1) {
			return;
		}
		final ServiceTypePanelModel serviceTypeModel = (ServiceTypePanelModel) this.tableModel;
		final ServiceType serviceType = serviceTypeModel.getRecord(selectedRow);
		final ServiceTypeEditor editor = new ServiceTypeEditor(this.controller, this.user, serviceType);
		final EditorDialog dialog = new EditorDialog(editor);

		if (dialog.isSaved()) {
			serviceTypeModel.setRecord(selectedRow, serviceType);
			this.tableModel.fireTableRowsUpdated(selectedRow, selectedRow);
			this.refresh();
		}
	}
}
